#include "GraphicEngine.h"
#pragma comment(lib,"Irrlicht.lib")

GraphicEngine::GraphicEngine(unsigned long Handle,s32 Width,s32 Height)
{
	params.WindowSize.Width=Width;
	params.WindowSize.Height=Height;
	params.WindowId = (void *) Handle;
	//params.DriverType = video::EDT_OPENGL;
	params.DriverType = video::EDT_DIRECT3D8; 
	params.Vsync=false;
	params.AntiAlias=false;
	params.Bits=16;
	params.Stencilbuffer = true;
	driver = NULL;
	device = NULL;

	InitEngine ();
	//Change font size
	if (Height<200)
	{
		labelFont = device->getGUIEnvironment()->getFont("media/font/font6px.bmp");
		fontSize = 6;
	}
	else if (Height<400)
	{
		labelFont = device->getGUIEnvironment()->getFont("media/font/font10px.bmp");
		fontSize = 10;
	}
	else if (Height<800)
	{
		labelFont = device->getGUIEnvironment()->getFont("media/font/font12px.bmp");
		fontSize = 12;
	}
	else
	{
		labelFont = device->getGUIEnvironment()->getFont("media/font/font16px.bmp");
		fontSize = 16;
	}
	numberFont = device->getGUIEnvironment()->getBuiltInFont();
	yLabelFont = device->getGUIEnvironment()->getFont("media/font/yFont.bmp");
}

void GraphicEngine::InitEngine(void)
{
	device = ::createDeviceEx(params);
	if (device!=0)
	{
		device->setResizeAble(true);
		driver = device->getVideoDriver();
	}
}

GraphicEngine::~GraphicEngine(void)
{

	
	if (device!=0)
	{
		device->closeDevice();
		device->run();
		device->grab();
		device->drop();
		delete device;
	}	
}

void GraphicEngine::draw2DDotLine(const irr::core::position2d<s32> &start, const irr::core::position2d<s32> &end, video::SColor color = video::SColor(255,255,255,255))
{
	int gapx = 10;
	int gapy = 7;
	int x,y;

	if (((end.X-start.X)/gapx)<10)
		gapx=0;
	if (((end.Y-start.Y)/gapy)<7)
		gapy=0;
	
	y =start.Y;
	x=start.X;
	if ((gapx==0) && (gapy==0)) return;
	do
	{
		if (((x+gapx) > end.X) && ((y+gapy>end.Y)))
		{
			driver->draw2DLine(core::position2d<s32>(x,y),core::position2d<s32>(end.X,end.Y),color);
			break;
		}
		else if (((x+gapx) > end.X) && (((y+gapy)<=end.Y)))
		{
			driver->draw2DLine(core::position2d<s32>(x,y),core::position2d<s32>(end.X,y+gapy),color);
			break;
		}
		else if (((x+gapx) <= end.X) && (((y+gapy)>end.Y)))
		{
			driver->draw2DLine(core::position2d<s32>(x,y),core::position2d<s32>(x+gapx,end.Y),color);
			break;
		}
		else
			driver->draw2DLine(core::position2d<s32>(x,y),core::position2d<s32>(x+gapx,y+gapy),color);
		x+=2*gapx;
		y+=2*gapy;
	}
	while (x<end.X || y<end.Y);
}

void GraphicEngine::draw2dPixel (core::position2di pos, video::SColor color)
{
	driver->draw2DLine(pos,core::position2di(pos.X,pos.Y+1),color);
}